package br.uvv.sao.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.uvv.sao.domain.OfertaDisciplina;
import br.uvv.sao.util.DAOException;

@SuppressWarnings({ "unchecked", "deprecation" })
public class OfertaDisciplinaDAO extends DAO {
	
	private static final long serialVersionUID = -7234154923271947197L;

	public void salvar(OfertaDisciplina ofertaInstance) throws DAOException {
		save(ofertaInstance, ofertaInstance.getId());
	}
	
	public void excluir(OfertaDisciplina ofertaInstance) throws DAOException {
		delete(OfertaDisciplina.class, ofertaInstance.getId());
	}
	
	public List<OfertaDisciplina> listar() throws DAOException {
		return list(OfertaDisciplina.class, null);
	}
	
	public OfertaDisciplina get(Long id) throws DAOException {
		return (OfertaDisciplina) get(OfertaDisciplina.class, id);
	}
	
	/**
	 * POG Get Primeira data de Um Ano!
	 * @param year
	 * @return
	 */
	private Date toStartOfYear(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * POG SEGUNDA DATA DO ANO
	 * @param year
	 * @return
	 */
	private Date toEndOfYear(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year+1);
		calendar.set(Calendar.DAY_OF_YEAR, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}
	
	public List<OfertaDisciplina> listar(Long idDisciplina, Long idProfessor, String semestre, Date ano) throws DAOException {
		Integer anoInt = ano.getYear();
		Date inicioAno = null;
		Date fimAno = null;
		if (anoInt < 0)
			anoInt = null;
		else {
			anoInt += 1900;
			inicioAno = toStartOfYear(anoInt);
			fimAno = toEndOfYear(anoInt);
		}
		
		Criterion[] filtros = new Criterion[4];
		if (idDisciplina > 0)
			filtros[0] = Restrictions.eq("disciplina.id", idDisciplina);
		if (idProfessor > 0)
			filtros[1] = Restrictions.eq("professor.id", idProfessor);
		if (!semestre.equals("0"))
			filtros[2] = Restrictions.eq("semestre", semestre);
		if (anoInt != null){
			filtros[3] = Restrictions.between("ano", inicioAno, fimAno);
		}
		String[] fetchMode = { "disciplina", "professor", "alunosMatriculados" };
		return list(OfertaDisciplina.class, fetchMode, Order.asc("id"), filtros);
	}
	
	public List<OfertaDisciplina> listar(int first, int pageSize,
			String sortField, boolean sortOrder, Long idDisciplina,
			Long idProfessor, String buscaSemestre, Date ano) throws DAOException {
		Integer anoInt = ano.getYear();
		Date inicioAno = null;
		Date fimAno = null;
		if (anoInt < 0)
			anoInt = null;
		else {
			anoInt += 1900;
			inicioAno = toStartOfYear(anoInt);
			fimAno = toEndOfYear(anoInt);
		}
		
		Criterion[] filtros = new Criterion[4];
		if (idDisciplina != null)
			filtros[0] = Restrictions.eq("disciplina", idDisciplina);
		if (idProfessor != null)
			filtros[1] = Restrictions.eq("professor", idProfessor);
		if (buscaSemestre != null)
			filtros[3] = Restrictions.eq("semestre", buscaSemestre);
		if (anoInt != null){
			filtros[3] = Restrictions.between("ano", inicioAno, fimAno);
		}
		
		String[] fetchMode = { "disciplina" , "professor" };
		return listPaginator(first, pageSize, sortField, sortOrder, OfertaDisciplina.class, fetchMode, filtros);
	}
}